#include "gofgame/dungeonkeeper/ui/ItemElementView.h"
#include "gofgame/common/ui/TableLayout.h"
#include "gofgame/common/ui/TableLayoutData.h"
#include "gofgame/common/ui/TableColumn.h"
#include "gofgame/common/graphics/LinearGradientBrush.h"

using namespace gofgame::dungeonkeeper::ui;
using namespace gofgame::common::graphics;

HornedTextureBrushResource ItemElementView::elementBrushResource(20, 20, "gofgame/dungeonkeeper/ui/element.png");
TextureResource ItemElementView::priceIconResource("gofgame/dungeonkeeper/ui/price_icon.png");

bool ItemElementView::init() {
	if (!Container::init()) {
		return false;
	}

	this->setBackgroundBrush(elementBrushResource.unwrap());
	this->setPadding(20);
	TableLayout *tableLayout = new TableLayout(30);
	this->setHeight(tableLayout->getMinRowHeight() * 2 + this->getPaddingTop() + this->getPaddingBottom());

	tableLayout->addColumn(TableColumn::relative(1));
	tableLayout->addColumn(TableColumn::relative(1));
	tableLayout->addColumn(TableColumn::relative(1));
	tableLayout->addColumn(TableColumn::relative(1));
	tableLayout->addColumn(TableColumn::relative(1));
	this->setLayout(tableLayout);
	TableLayoutData *tableLayoutData;

	this->image.initialize();
	this->image->init();
	this->image->setResourceName("Hero/stand_6.png");
	tableLayoutData = new TableLayoutData();
	tableLayoutData->setRowSpan(2);
	this->image->setLayoutData(tableLayoutData);
	this->addChild(this->image);

	Ref<Label> nameDescLabel;
	nameDescLabel.initialize();
	nameDescLabel->init();
	nameDescLabel->setText("Name: ");
	nameDescLabel->setHorizontalAlignment(HorizontalAlignment_Right);
	this->addChild(nameDescLabel);

	this->nameLabel.initialize();
	this->nameLabel->init();
	this->nameLabel->setText("Bom");
	this->nameLabel->setHorizontalAlignment(HorizontalAlignment_Left);
	this->addChild(this->nameLabel);

	Ref<Image> priceDescImage;
	priceDescImage.initialize();
	priceDescImage->init();
	priceDescImage->setTexture(priceIconResource.unwrap());
	priceDescImage->setDisplayMode(ImageDisplayMode_Right);
	this->addChild(priceDescImage);

	this->priceLabel.initialize();
	this->priceLabel->init();
	this->priceLabel->setText("200");
	this->priceLabel->setHorizontalAlignment(HorizontalAlignment_Left);
	this->addChild(this->priceLabel);

	Ref<Label> descriptionDescLabel;
	descriptionDescLabel.initialize();
	descriptionDescLabel->init();
	descriptionDescLabel->setText("description: ");
	descriptionDescLabel->setHorizontalAlignment(HorizontalAlignment_Right);
	this->addChild(descriptionDescLabel);

	this->descriptionLabel.initialize();
	this->descriptionLabel->init();
	this->descriptionLabel->setText("Kill the unit in a area");
	this->descriptionLabel->setHorizontalAlignment(HorizontalAlignment_Left);
	tableLayoutData = new TableLayoutData();
	tableLayoutData->setColSpan(3);
	this->descriptionLabel->setLayoutData(tableLayoutData);
	this->addChild(this->descriptionLabel);

	return true;
}

ItemElementViewInfo ItemElementView::getItemElementViewInfo() const {
	ItemElementViewInfo ievi;
	ievi.imagePath = this->image->getResourceName();
	ievi.name = this->nameLabel->getText();
	ievi.desc = this->descriptionLabel->getText();
	sscanf(this->priceLabel->getText(), "%d", &ievi.price);
	return ievi;
}

void ItemElementView::setItemElementViewInfo(const ItemElementViewInfo &ievi) {
	char text[32];
	this->image->setResourceName(ievi.imagePath.c_str());
	this->nameLabel->setText(ievi.name.c_str());
	this->descriptionLabel->setText(ievi.desc.c_str());
	sprintf(text, "%d", ievi.price);
	this->priceLabel->setText(text);
}